package com.das.mechanic_base.mvp.view.camera;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.R2;
import com.das.mechanic_base.base.X3BaseActivity;
import com.das.mechanic_base.base.X3BasePresenter;
import com.das.mechanic_base.bean.alone.CameraCropBean;
import com.das.mechanic_base.utils.X3FileUtils;
import com.das.mechanic_base.widget.X3CropImageView;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

public class X3CropActivity extends X3BaseActivity {

    @BindView(R2.id.cv_view)
    X3CropImageView cv_view;
    @BindView(R2.id.tv_cancel)
    TextView tv_cancel;
    @BindView(R2.id.tv_reset)
    TextView tv_reset;
    @BindView(R2.id.tv_ok)
    TextView tv_ok;

    private String cropPath;
    private int imgHeight;


    @Override
    protected int getLayoutId() {
        return R.layout.x3_vactivity_crop;
    }

    @Override
    protected void initView() {
        cropPath = getIntent().getStringExtra("cropPath");

        imgHeight = getIntent().getIntExtra("imgHeight", 0);

        if (imgHeight != 0) {
            ViewGroup.LayoutParams layoutParams = cv_view.getLayoutParams();
            layoutParams.height = imgHeight;
            cv_view.setLayoutParams(layoutParams);
        }

        int cropRota = getIntent().getIntExtra("cropRota", 0);

        cv_view.setRotation(cropRota);

        cv_view.setImageUriAsync(Uri.fromFile(new File(cropPath)));

    }

    @Override
    protected X3BasePresenter createPresenter() {
        return null;
    }

    @OnClick({R2.id.tv_cancel,R2.id.tv_reset,R2.id.tv_ok})
    public void onViewClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_cancel) {
            supportFinishAfterTransition();
        } else if (id == R.id.tv_reset) {
            cv_view.resetCropRect();
        } else if (id == R.id.tv_ok) {
            Bitmap croppedImage = cv_view.getCroppedImage();
            X3FileUtils.saveOriginBitmapCompress(this, croppedImage, "", path -> {
                CameraCropBean cropBean = new CameraCropBean();
                cropBean.type = 2;
                cropBean.beforePath = cropPath;
                cropBean.path = path;
                EventBus.getDefault().post(cropBean);
            });
            supportFinishAfterTransition();
        }
    }

    @Override
    public void supportFinishAfterTransition() {
        Intent intent = getIntent();
        intent.putExtra("cropPath",cropPath);
        setResult(RESULT_OK, intent);
        super.supportFinishAfterTransition();
    }

    @Override
    public void onBackPressed() {
        Intent intent = getIntent();
        intent.putExtra("cropPath",cropPath);
        setResult(RESULT_OK, intent);
        super.supportFinishAfterTransition();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("图片裁剪页");
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("图片裁剪页");
    }
}